import Vue from 'vue'
import VueRouter from 'vue-router'


// import Login from '../components/Login.vue';
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome.vue')

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/components/power/Roles.vue')

const Categories = () => import(/* webpackChunkName: "Categories_Params" */ '@/components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Categories_Params" */ '@/components/goods/Params.vue')

const List = () => import(/* webpackChunkName: "List_Add" */ '@/components/goods/List.vue')
const Add = () => import(/* webpackChunkName: "List_Add" */ '@/components/goods/Add.vue')

const Order = () => import(/* webpackChunkName: "Order_Report" */ '@/components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '@/components/report/Report.vue')

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  // 重定向
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/home",
    name: "Home",
    redirect: "/welcome",
    component: Home,
    children: [
      {
        path: "/welcome",
        name: "Welcome",
        component: Welcome,
      },
      {
        path: "/users",
        name: "Users",
        component: Users,
      },
      // 权限管理
      {
        path: "/rights",
        name: "Rights",
        component: Rights,
      },
      //
      {
        path: "/roles",
        name: "Roles",
        component: Roles,
      },
      // 商品管理
      {
        path: "/categories",
        name: "Categories",
        component: Categories,
      },
      {
        path: '/params',
        name: 'Params',
        component: Params
      },
      // 
      {
        path: '/goods',
        name: 'List',
        component: List
      },
      {
        path: '/goods/add',
        name: 'Add',
        component: Add
      },
      // 
      {
        path: '/orders',
        name: 'Order',
        component: Order
      },
      {
        path: '/reports',
        name: 'Report',
        component: Report
      }
      
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 挂载全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  // 获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login");
  next();
});

router.afterEach((to) => {
  window.sessionStorage.setItem("activePath", to.path);
});

export default router
